<template>
  <div class="app-container">
    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="600px" v-dialogDrag append-to-body>
      <el-form ref="formRef" :model="formData" :rules="formRules" v-loading="formLoading" label-width="100px">
        <!-- 使用el-row和el-col实现一行两列布局 -->
        <el-row :gutter="10" style="display:flex;flex-wrap: wrap">
          <el-col :span="12">
            <el-form-item label="段别" prop="segmentId">
              <el-select v-model="formData.segmentId" placeholder="请选择段别" style="width: 100%" @change="handleSegmentChange">
                <el-option v-for="item in segmentList" :key="item.id" :label="item.name" :value="item.id"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="物料编号" prop="materialCode">
              <el-input v-model="formData.materialCode" placeholder="请输入物料编号" style="width: 100%"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="标准产能" prop="standardCapacity">
              <el-input v-model="formData.standardCapacity" placeholder="请输入标准产能" style="width: 100%"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="标准人力" prop="standardManpower">
              <el-input v-model="formData.standardManpower" placeholder="请输入标准人力" style="width: 100%"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="插件数" prop="pluginsNumber">
              <el-input v-model="formData.pluginsNumber" placeholder="请输入插件数" style="width: 100%"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="连片数" prop="contiguousNumber">
              <el-input v-model="formData.contiguousNumber" placeholder="请输入连片数" style="width: 100%"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :disabled="formLoading">确 定</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import * as SegmentMaterialApi from '@/api/lamp/segmentmaterial';
import * as SegmentApi from '@/api/lamp/segment';

export default {
  name: "SegmentMaterialForm",
  components: {},
  data() {
    return {
      // 弹出层标题
      dialogTitle: "新增/修改段别物料绑定",
      // 是否显示弹出层
      dialogVisible: false,
      // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
      formLoading: false,
      // 段别列表
      segmentList: [],
      // 表单参数
      formData: {
        id: undefined,
        title: undefined,
        segmentId: undefined,
        segmentName: undefined,
        materialCode: undefined,
        standardCapacity: undefined,
        standardManpower: undefined,
        pluginsNumber: undefined,
        contiguousNumber: undefined,
      },
      // 表单校验
      formRules: {
        title: [{ required: true, message: '标题不能为空', trigger: 'blur' }],
        segmentId: [{ required: true, message: '段别不能为空', trigger: 'blur' }],
        materialCode: [{ required: true, message: '物料编号不能为空', trigger: 'blur' }],
        standardCapacity: [{ required: true, message: '标准产能不能为空', trigger: 'blur' }],
        standardManpower: [{ required: true, message: '标准人力不能为空', trigger: 'blur' }],
      },
    };
  },
  methods: {
    /** 打开弹窗 */
    async open(id) {
      this.dialogVisible = true;
      this.reset();
      // 获取段别列表
      await this.getSegmentList();
      // 修改时，设置数据
      if (id) {
        this.formLoading = true;
        try {
          const res = await SegmentMaterialApi.getSegmentMaterial(id);
          this.formData = res.data;
        } finally {
          this.formLoading = false;
        }
      }
    },
    
    /** 获取段别列表 */
    async getSegmentList() {
      try {
        const res = await SegmentApi.getSegmentPage({ pageSize: 100, pageNo: 1 });
        this.segmentList = res.data.list;
      } catch (error) {
        this.$message.error('获取段别列表失败');
      }
    },
    
    /** 处理段别选择 */
    handleSegmentChange(segmentId) {
      if (segmentId) {
        const selectedSegment = this.segmentList.find(item => item.id === segmentId);
        if (selectedSegment) {
          this.formData.segmentName = selectedSegment.name;
        } else {
          this.formData.segmentName = undefined;
        }
      } else {
        this.formData.segmentName = undefined;
      }
    },
    
    /** 提交按钮 */
    async submitForm() {
      // 校验主表
      await this.$refs["formRef"].validate();
      this.formLoading = true;
      try {
        const data = this.formData;
        // 修改的提交
        if (data.id) {
          await SegmentMaterialApi.updateSegmentMaterial(data);
          this.$modal.msgSuccess("修改成功");
          this.dialogVisible = false;
          this.$emit('success');
          return;
        }
        // 添加的提交
        await SegmentMaterialApi.createSegmentMaterial(data);
        this.$modal.msgSuccess("新增成功");
        this.dialogVisible = false;
        this.$emit('success');
      } finally {
        this.formLoading = false;
      }
    },
    
    /** 表单重置 */
    reset() {
      this.formData = {
        id: undefined,
        title: undefined,
        segmentId: undefined,
        segmentName: undefined,
        materialCode: undefined,
        standardCapacity: undefined,
        standardManpower: undefined,
        pluginsNumber: undefined,
        contiguousNumber: undefined,
      };
      this.resetForm("formRef");
    }
  }
};
</script>

<style scoped>
/* 确保表单元素宽度一致 */
.el-input,
.el-select {
  width: 100%;
}
</style>
